/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.settlement.service.investorcarcontract;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.custom.service.CodeRuleService;
import com.jeesite.modules.file.utils.FileUploadUtils;
import com.jeesite.modules.order.service.serviceorder.ServiceOrderService;
import com.jeesite.modules.settlement.dao.investorcarcontract.InvestorCarContractDao;
import com.jeesite.modules.settlement.dao.investorcarcontract.InvestorCarContractDetailDao;
import com.jeesite.modules.settlement.entity.investorcarcontract.InvestorCarContract;
import com.jeesite.modules.settlement.entity.investorcarcontract.InvestorCarContractDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 资方汽车供需合同Service
 * @author LV
 * @version 2021-03-06
 */
@Service
@Transactional(readOnly=true)
public class InvestorCarContractService extends CrudService<InvestorCarContractDao, InvestorCarContract> {
	
	@Autowired
	private InvestorCarContractDetailDao investorCarContractDetailDao;
	@Autowired
	private ServiceOrderService serviceOrderService;
    @Autowired
    private CodeRuleService codeRuleService;
	/**
	 * 获取单条数据
	 * @param investorCarContract
	 * @return
	 */
	@Override
	public InvestorCarContract get(InvestorCarContract investorCarContract) {
		InvestorCarContract entity = super.get(investorCarContract);
		if (entity != null){
			InvestorCarContractDetail investorCarContractDetail = new InvestorCarContractDetail(entity);
			investorCarContractDetail.setStatus(InvestorCarContractDetail.STATUS_NORMAL);
			entity.setInvestorCarContractDetailList(investorCarContractDetailDao.findList(investorCarContractDetail));
		}
		return entity;
	}
	
	/**
	 * 查询分页数据
	 * @param investorCarContract 查询条件
	 * @param investorCarContract.page 分页对象
	 * @return
	 */
	@Override
	public Page<InvestorCarContract> findPage(InvestorCarContract investorCarContract) {
		return super.findPage(investorCarContract);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param investorCarContract
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(InvestorCarContract investorCarContract) {
        if (investorCarContract.getIsNewRecord()){
            investorCarContract.setInvestorCarContractCode(codeRuleService.getCode("investorcarcontract"));
        }
		super.save(investorCarContract);
        if (investorCarContract.getIsNewRecord()){
            codeRuleService.updateCurrentNumber("investorcarcontract");
        }
		// 保存上传附件
		FileUploadUtils.saveFileUpload(investorCarContract.getId(), "investorCarContract_file");
		// 保存 InvestorCarContract子表
		for (InvestorCarContractDetail investorCarContractDetail : investorCarContract.getInvestorCarContractDetailList()){
			if (!InvestorCarContractDetail.STATUS_DELETE.equals(investorCarContractDetail.getStatus())){
				investorCarContractDetail.setInvestorCarContractId(investorCarContract);
				if (investorCarContractDetail.getIsNewRecord()){
					investorCarContractDetailDao.insert(investorCarContractDetail);
                    serviceOrderService.updateInvestorCarContract(investorCarContract.getId(),investorCarContractDetail.getFrameNumber());
				}else{
					investorCarContractDetailDao.update(investorCarContractDetail);
                    serviceOrderService.updateInvestorCarContract(investorCarContract.getId(),investorCarContractDetail.getFrameNumber());
				}
			}else{
				investorCarContractDetailDao.delete(investorCarContractDetail);
                serviceOrderService.updateInvestorCarContract("",investorCarContractDetail.getFrameNumber());
			}
		}
	}
	
	/**
	 * 更新状态
	 * @param investorCarContract
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(InvestorCarContract investorCarContract) {
		super.updateStatus(investorCarContract);
	}
	
	/**
	 * 删除数据
	 * @param investorCarContract
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(InvestorCarContract investorCarContract) {
		super.delete(investorCarContract);
		InvestorCarContractDetail investorCarContractDetail = new InvestorCarContractDetail();
		investorCarContractDetail.setInvestorCarContractId(investorCarContract);
		investorCarContractDetailDao.deleteByEntity(investorCarContractDetail);
	}
	
}